<template>
  <div class="login-container">
    <!-- 左侧介绍区域 -->
    <div class="login-left">
      <div class="left-content">
        <div class="logo-section">
          <n-icon size="64" color="#ffffff">
            <HeartOutline />
          </n-icon>
          <h1 class="system-title">社区志愿者服务管理系统</h1>
          <p class="system-subtitle">Community Volunteer Service Management System</p>
        </div>

        <div class="features">
          <div class="feature-item">
            <n-icon size="32" color="#ffffff">
              <PeopleOutline />
            </n-icon>
            <div class="feature-text">
              <h3>志愿者管理</h3>
              <p>完善的志愿者信息管理与服务记录</p>
            </div>
          </div>

          <div class="feature-item">
            <n-icon size="32" color="#ffffff">
              <CalendarOutline />
            </n-icon>
            <div class="feature-text">
              <h3>活动组织</h3>
              <p>便捷的活动发布与报名管理功能</p>
            </div>
          </div>

          <div class="feature-item">
            <n-icon size="32" color="#ffffff">
              <TrophyOutline />
            </n-icon>
            <div class="feature-text">
              <h3>积分激励</h3>
              <p>科学的积分制度激发志愿者热情</p>
            </div>
          </div>

          <div class="feature-item">
            <n-icon size="32" color="#ffffff">
              <StatsChartOutline />
            </n-icon>
            <div class="feature-text">
              <h3>数据统计</h3>
              <p>直观的数据分析与可视化展示</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="login-form-wrapper">
        <div class="form-header">
          <h2>欢迎登录</h2>
          <p>Welcome Back</p>
        </div>

        <n-form ref="formRef" :model="formValue" :rules="rules" size="large">
          <n-form-item path="username">
            <n-input
              v-model:value="formValue.username"
              placeholder="请输入用户名"
              @keydown.enter="handleLogin"
            >
              <template #prefix>
                <n-icon :component="PersonOutline" />
              </template>
            </n-input>
          </n-form-item>

          <n-form-item path="password">
            <n-input
              v-model:value="formValue.password"
              type="password"
              show-password-on="click"
              placeholder="请输入密码"
              @keydown.enter="handleLogin"
            >
              <template #prefix>
                <n-icon :component="LockClosedOutline" />
              </template>
            </n-input>
          </n-form-item>

          <n-form-item>
            <n-button
              type="primary"
              block
              size="large"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </n-button>
          </n-form-item>
        </n-form>

        <div class="form-footer">
          <n-divider style="margin: 20px 0">演示账号</n-divider>
          <n-space vertical size="small">
            <div class="demo-account">
              <n-tag type="error" size="small">管理员</n-tag>
              <n-text depth="3">admin / 123456</n-text>
            </div>
            <div class="demo-account">
              <n-tag type="success" size="small">志愿者</n-tag>
              <n-text depth="3">volunteer1 / 123456</n-text>
            </div>
          </n-space>
          
          <n-divider style="margin: 20px 0" />
          
          <div class="register-link">
            <n-text depth="3">还没有账号？</n-text>
            <n-button text type="primary" @click="router.push('/register')">
              立即注册
            </n-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { login } from '@/api/auth'
import { 
  PersonOutline, 
  LockClosedOutline, 
  HeartOutline, 
  PeopleOutline, 
  CalendarOutline, 
  TrophyOutline, 
  StatsChartOutline 
} from '@vicons/ionicons5'

const router = useRouter()
const userStore = useUserStore()

const formRef = ref(null)
const loading = ref(false)
const formValue = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = () => {
  formRef.value?.validate(async (errors) => {
    if (errors) return

    loading.value = true
    try {
      const res = await login(formValue.value)
      userStore.setUserInfo(res.data)
      window.$message?.success('登录成功')
      router.push('/dashboard')
    } catch (error) {
      console.error('登录失败:', error)
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  overflow: hidden;
}

/* 左侧介绍区域 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #18A058 0%, #0C7A43 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -20%;
  width: 600px;
  height: 600px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 50%;
}

.login-left::after {
  content: '';
  position: absolute;
  bottom: -30%;
  left: -10%;
  width: 400px;
  height: 400px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 50%;
}

.left-content {
  position: relative;
  z-index: 1;
  max-width: 500px;
  width: 100%;
}

.logo-section {
  text-align: center;
  margin-bottom: 60px;
}

.system-title {
  color: #ffffff;
  font-size: 32px;
  font-weight: 700;
  margin: 20px 0 10px;
  line-height: 1.4;
}

.system-subtitle {
  color: rgba(255, 255, 255, 0.85);
  font-size: 14px;
  letter-spacing: 1px;
  margin: 0;
}

.features {
  display: grid;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.feature-item:hover {
  background: rgba(255, 255, 255, 0.12);
  transform: translateX(8px);
}

.feature-text h3 {
  color: #ffffff;
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.feature-text p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0;
  line-height: 1.6;
}

/* 右侧登录表单 */
.login-right {
  width: 480px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  padding: 40px;
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.08);
}

.login-form-wrapper {
  width: 100%;
  max-width: 380px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #18A058;
  margin: 0 0 8px;
}

.form-header p {
  font-size: 14px;
  color: #999;
  margin: 0;
}

.form-footer {
  margin-top: 24px;
}

.demo-account {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
}

.register-link {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-left {
    padding: 40px;
  }
  
  .login-right {
    width: 420px;
  }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    flex: none;
    min-height: 300px;
    padding: 30px;
  }
  
  .left-content {
    max-width: 100%;
  }
  
  .system-title {
    font-size: 24px;
  }
  
  .features {
    display: none;
  }
  
  .login-right {
    width: 100%;
    flex: 1;
  }
}
</style>

